<?php

class Download {
	public $path;
	public $version;
	public $uri;
	public $fileName;
	public $fileModTime;
}

class DownloadsBean {
	public $downloadsPath;
	
	public function __construct() {
		$this->downloadsPath = "downloads/";
	}
	
	public function getLatestSmarty() {
		return $this->getLatest( "HTML_Template_Smarty" );
	}
	
	public function getLatestPhpFaces() {
		return $this->getLatest( "PhpFaces" );
	}
	
	protected function getLatest( $package ) {
		$pattern = sprintf( "/%s-([\d\.]+)\.tgz/i", $package );
		
		$download = null;
		
		$dir = opendir( $this->downloadsPath );
		while( $f = readdir( $dir ) ) {
			if( preg_match( $pattern, $f, $m ) ) {
				$version = $m[1];
				
				if( $download == null || version_compare( $version, $download->version, ">" ) ) {
					$download = new Download();
					$download->version = $version;
					$download->path = $this->downloadsPath.$f;
					$download->uri = $download->path;
					$download->fileName = $f;
					$download->fileModTime = filemtime( $download->path );
				}
			}
		}
		closedir( $dir );
		
		return $download;
	}	
}

?>